﻿using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week1.Warehouse_CRUD
{
    /// <summary>
    /// 删除仓库
    /// </summary>
    public class DelWarehouseHandler : IRequestHandler<DelWarehouseCommand, Result>
    {
        public DelWarehouseHandler(IBaseRepository<Warehouse> warehouseRepository)
        {
            WarehouseRepository = warehouseRepository;
        }

        public IBaseRepository<Warehouse> WarehouseRepository { get; set; }
        public async Task<Result> Handle(DelWarehouseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result() { Code = (int)CommonErrorCodes.Delete };
                string Selectsql = "select * from Warehouse where WarehouseId=@WarehouseId";
                var list = WarehouseRepository.GetAll(Selectsql, new { WarehouseId = request.WarehouseId }).FirstOrDefault();
                if (list != null)
                {
                    var WarehouseIsDel = list.IsDelete == true ? false : true;
                    string UpSql = "update Warehouse set WarehouseIsDel=@WarehouseIsDel where WarehouseId=@WarehouseId";
                    var UpNum = WarehouseRepository.Update(UpSql, new { WarehouseIsDel = WarehouseIsDel, WarehouseId = request.WarehouseId });
                    if (UpNum > 0)
                    {
                        result.Code = (int)CommonErrorCodes.Success;
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                else
                {
                    return result;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
